package com.example.dangerouscargodriver.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.baiju.bjpopupwindow.Item.DLPopItem;
import com.baiju.bjpopupwindow.view.DLPopupWindow;
import com.baiju.netmanager.Http.IHttpRequest;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.entry.BankAccount;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.example.dangerouscargodriver.R;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import baiju.kit.datetimepicker.widget.DateTimePicker;

public class BillDetailActivity extends HttpRequestActivity implements View.OnClickListener {

    private static final String TAG = "BillDetailActivity";
    private ActionBar mActionBar = null;

    private LinearLayout mLayoutRoleSelector = null;
    private DLPopupWindow mRolePopupWindow;
    private List<DLPopItem> mRoleList = new ArrayList<>();

    private LinearLayout mLayoutBillTypeSelector = null;
    private DLPopupWindow mBillTypePopupWindow;
    private List<DLPopItem> mBillTypeList = new ArrayList<>();

    private LinearLayout mLayoutBillDateSelector = null;
    private DateTimePicker mBillDatePicker = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initHttpComponent(BillDetailActivity.class);
        setContentView(R.layout.activity_bill_detail);

        initRolePopupWindow();
        mLayoutRoleSelector = findViewById(R.id.selector_choose_role);
        mLayoutRoleSelector.setOnClickListener(this);

        initBillTypePopupWindow();
        mLayoutBillTypeSelector = findViewById(R.id.selector_choose_bill_type);
        mLayoutBillTypeSelector.setOnClickListener(this);

        mLayoutBillDateSelector = findViewById(R.id.selector_choose_time);
        mLayoutBillDateSelector.setOnClickListener(this);

        mActionBar = findViewById(R.id.actionBarBillDetail);
        mActionBar.setOnBackClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        executeGetBillDetail();
    }

    private void initRolePopupWindow() {
        DLPopItem item = new DLPopItem(0, "全部", 0x000000);
        mRoleList.add(item);
        item = new DLPopItem(0, "货主", 0x000000);
        mRoleList.add(item);
        item = new DLPopItem(0, "物流商", 0x000000);
        mRoleList.add(item);

        mRolePopupWindow = new DLPopupWindow(this, mRoleList, DLPopupWindow.STYLE_DEF);

        mRolePopupWindow.setOnItemClickListener(new DLPopupWindow.OnItemClickListener() {
            @Override
            public void OnClick(int position) {
                if(position == 0){
                }
                else if(position == 1){

                }else{

                }
            }
        });
    }

    private void initBillTypePopupWindow() {
        DLPopItem item = new DLPopItem(0, "全部", 0x000000);
        mBillTypeList.add(item);
        item = new DLPopItem(0, "收入", 0x000000);
        mBillTypeList.add(item);
        item = new DLPopItem(0, "支出", 0x000000);
        mBillTypeList.add(item);
        item = new DLPopItem(0, "退款", 0x000000);
        mBillTypeList.add(item);


        mBillTypePopupWindow = new DLPopupWindow(this, mBillTypeList, DLPopupWindow.STYLE_DEF);

        mBillTypePopupWindow.setOnItemClickListener(new DLPopupWindow.OnItemClickListener() {
            @Override
            public void OnClick(int position) {
                if(position == 0){
                    /**/
                }
                else if(position == 1){
                    /**/
                }else{
                    /**/
                }
            }
        });
    }
    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.selector_choose_role){
            mRolePopupWindow.showAsDropDown(v, 0, 30);
        }
        else if(v.getId() == R.id.selector_choose_bill_type){
            mBillTypePopupWindow.showAsDropDown(v, 0, 20);
        }
        else if(v.getId() == R.id.selector_choose_time){
            Calendar calendar = Calendar.getInstance();
            Date startDate = calendar.getTime();

            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 8);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            Date endDate = calendar.getTime();

            DateTimePicker.Builder builder = new DateTimePicker.Builder(BillDetailActivity.this)
                    .setTitle("选择年月")
                    .setCancelTextColor(Color.GRAY)
                    .setOkTextColor(getResources().getColor(R.color.base_yellow))
                    .setTitleTextColor(0xFF999999)
                    .setSelectedTextColor(getResources().getColor(R.color.base_yellow))
                    .setKeepLastSelected(true)
                    .setShowYMDHMLabel(true)
                    .setShowType(DateTimePicker.ShowType.MONTH);
            mBillDatePicker = new DateTimePicker(BillDetailActivity.this, new DateTimePicker.ResultHandler() {
                @Override
                public void handle(Date date) {
                    /**/
                }
            }, startDate, endDate, builder);
            mBillDatePicker.show(startDate);
        }
        else{

        }
    }

    private void executeGetBillDetail(){
        HashMap<String, String> header = new HashMap<String, String>();
        header.put("token", AuthController.Instance().getToken());

        sendHttpRequest(
                IHttpRequest.RequestMethod.GET_METHOD,
                RemoteAPI.REQUEST_V1_BILL_DETAIL,
                header,
                null,
                RemoteAPICmd.REQUEST_V1_BILL_DETAIL);
    }

    @Override
    protected void onHttpResponse(int actionId, String response) {
        try {
            JSONObject res = new JSONObject(response);
            Gson gson = new Gson();
            if(actionId == RemoteAPICmd.REQUEST_V1_BILL_DETAIL){
                Log.d(TAG, "onResponse: " + response);
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                if(responseInfo != null && responseInfo.getStatus() == 1){

                }
                else{

                }
            }
            else{

            }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onHttpErrorResponse(int actionId, String response) {

    }
}